package com.zyg.controller;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zyg.pojo.Hotel;
import com.zyg.service.HotelService;
import com.zyg.util.LayData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/hotel")
public class HotelController {
    @Autowired
    private HotelService hotelService;
    @RequestMapping("/tolist")
    public String tolist(){
        return "hotel/list";
    }
    @RequestMapping("/list")
    @ResponseBody
    public String list(Hotel hotel, @RequestParam(value = "page", defaultValue = "1") Integer page, @RequestParam(value = "limit", defaultValue = "10") Integer limit){
        LayData layData = new LayData();
        QueryWrapper<Hotel> qw=new QueryWrapper<>();
        qw.like(hotel.getHotelName()!=null && !"".equals(hotel.getHotelName()),"hotel_name",hotel.getHotelName());
        long count = hotelService.count(qw);
        layData.setCount((int)count);
        //查询数据
        Page<Hotel> page1=new Page<>(page,limit);
       layData.setData(hotelService.list(page1, qw));
        layData.setCode(0);
        layData.setMsg("查询成功");
        String data = JSON.toJSONString(layData);
        return data;
    }
    @RequestMapping("/toinfo/{id}")
    public String toInfo(@PathVariable("id") int id, Model model){
        Hotel hotel = hotelService.getById(id);
        model.addAttribute("hotel", hotel);
        return "hotel/info";
    }
    @RequestMapping("/toadd")
    public String toAdd(){
        return "hotel/add";
    }
    @RequestMapping("/addSave")
    @ResponseBody
    public String addSave(Hotel hotel){
        boolean result= hotelService.save(hotel);
        if(result){
            return JSON.toJSONString(new LayData(0, "添加成功"));
        }else{
            return JSON.toJSONString(new LayData(500, "添加失败"));
        }
    }
    @RequestMapping("/toupdate/{id}")
    public String toUpdate(@PathVariable("id") int id, Model model){
        Hotel hotel = hotelService.getById(id);
        model.addAttribute("hotel", hotel);
        return "hotel/update";
    }
    @RequestMapping("/updateSave")
    @ResponseBody
    public String updateSave(Hotel hotel){
        boolean result = hotelService.updateById(hotel);
        if(result){
            return JSON.toJSONString(new LayData(0,"修改成功"));
        }else {
            return JSON.toJSONString(new LayData(500,"修改失败"));
        }
    }
    @RequestMapping("/delete/{id}")
    @ResponseBody
    public String deleteHotel(@PathVariable("id") int id){
        boolean result= hotelService.removeById(id);
        if(result){
            return JSON.toJSONString(new LayData(0, "删除成功"));
        }else{
            return JSON.toJSONString(new LayData(500, "删除失败"));
        }
    }
}
